package com.xnew.shiro;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.xnew.user.service.AuUserService;
import com.xnew.bean.common.SysUserInfo;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import com.xnew.bean.common.AuUserRole;

/**
 * Created by lenovo on  三月
 */
public class UserRealm extends AuthorizingRealm {
    @Autowired
    private AuUserService auUserService;


    /**
     * 提供用户信息，返回权限信息
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SimpleAuthorizationInfo authorizationInfo=new SimpleAuthorizationInfo();
        String userName=(String) principals.getPrimaryPrincipal();
        String userId=auUserService.findUserByName(userName).getUserInfoNo();
        List<AuUserRole> roleIdSet=auUserService.findRoleIdByUid(userId);
        Set<String> roleSet=new HashSet<String>();
        List<String>  pemissionIdSet=new ArrayList<String>();
        Set<String>  pemissionSet=new HashSet<String>();
        for(AuUserRole roleInfo : roleIdSet) {
              String roleId=roleInfo.getArId();
              roleSet.add(auUserService.findRoleByRoleId(roleId));
               //将拥有角色的所有权限放进Set里面，也就是求Set集合的并集
              pemissionIdSet.addAll(auUserService.findPermissionIdByRoleId(roleId));
        }
        for(String permissionId : pemissionIdSet) {
            String permission= auUserService.findPermissionById(permissionId).getArsUrl() ;
            pemissionSet.add(permission);
        }
         // 将角色名称提供给授权info
        authorizationInfo.setRoles(roleSet);
        // 将权限名称提供给info
        authorizationInfo.setStringPermissions(pemissionSet);

        return authorizationInfo;
    }

    /**
     * 提供帐户信息，返回认证信息
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String userName = (String)authenticationToken.getPrincipal();
        SysUserInfo user = auUserService.findUserByName(userName);
        if(user == null) {
            //用户不存在就抛出异常
            throw new UnknownAccountException();
        }
//        if("01".equals(user.getStatus())) {
//           //用户被锁定就抛异常
//           throw new  LockedAccountException();
//        }
        //密码可以通过SimpleHash加密，然后保存进数据库。
        //此处是获取数据库内的账号、密码、盐值，保存到登陆信息info中
        SimpleAuthenticationInfo authenticationInfo=new SimpleAuthenticationInfo(user.getLoginName(),
                user.getPwd()+user.getSalt(),
                getName());                   //realm name

        return authenticationInfo;
    }
}
